package com.rew.canteenchefs.managers.imp;

import com.rew.canteenchefs.daos.IEDishesDao;
import com.rew.canteenchefs.managers.IEDishesManager;
import com.rew.canteenchefs.models.EDishes;
import com.rew.canteenchefs.models.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import static com.rew.canteenchefs.enumerate.ResultTypeEnum.*;

/**
 * DishesManager.
 *
 * Created by HuiWen Ren on 2016/12/9.
 */
@Component
public class EDishesManager implements IEDishesManager{
    private static final Logger LOGGER = LoggerFactory
            .getLogger(EDishesManager.class);

    @Autowired
    IEDishesDao dishesDao;

    /**
     * 插入菜品.
     *
     * @param eDishes dishes实体
     * @return 插入结果
     */
    @Override
    public Result<Integer> insertDishes(EDishes eDishes) {
        Result<Integer> result = new Result<>();
        try {
            result.setBean(dishesDao.insertDishes(eDishes));
            if(result.getBean().equals(SUCCESS.getValue())){
                result.setCode(SUCCESS.getValue());
                result.setMessage(SUCCESS.getDescription());
            }

        } catch(DuplicateKeyException e) {
            LOGGER.error("DISHES_DuplicateKeyFAILED", e);
            result.setCode(DUPLICATE.getValue());
            result.setMessage(DUPLICATE.getDescription());

        } catch(RuntimeException e){
            LOGGER.error("DISHES_InsertFAILED", e);
            result.setCode(FAILED.getValue());
            result.setMessage(FAILED.getDescription());
        }
        return result;
    }

    /**
     *查询菜品信息.
     *
     * @param dishName 待查询菜品名称
     * @return 有则返回，否则为空
     */

    @Override
    public Result<EDishes> selectDishes(String dishName) {
        Result<EDishes> result = new Result<>();
        try {
            result.setBean(dishesDao.selectDishes(dishName));
            if(result.getBean() != null){
                result.setCode(SUCCESS.getValue());
                result.setMessage(SUCCESS.getDescription());
            } else {
                result.setCode(NOT_EXIST.getValue());
                result.setMessage(NOT_EXIST.getDescription());
            }
        } catch(RuntimeException e){
            LOGGER.error("DISHES_SelectFAILED", e);
            result.setCode(FAILED.getValue());
            result.setMessage(FAILED.getDescription());
        }
        return result;
    }

    /**
     * 更新菜品信息.
     *
     * @param dishes 更新菜品
     * @return 更新结果
     */
    @Override
    public Result<Integer> updateDishes(EDishes dishes) {
        Result<Integer> result = new Result<>();
        try {
            result.setBean(dishesDao.updateDishes(dishes));
            result.setCode(result.getBean());
            result.setMessage(SUCCESS.getDescription());
        } catch (RuntimeException e){
            LOGGER.error("DISHES_UpdateFAILED:", e);
            result.setMessage(FAILED.getDescription());
            result.setCode(FAILED.getValue());
        }
        return result;
    }

    /**
     * 菜品删除.
     *
     * @param dishName 菜品名称
     * @return 删除结果
     */
    @Override
    public Result<Integer> deleteDishes(String dishName) {
        Result<Integer> result = new Result<>();
        try {
            result.setBean(dishesDao.deleteDishes(dishName));
            result.setCode(result.getBean());
            result.setMessage(SUCCESS.getDescription());
        } catch (RuntimeException e){
            LOGGER.error("DISHES_DeleteFAILED:", e);
            result.setMessage(FAILED.getDescription());
            result.setCode(FAILED.getValue());
        }
        return result;
    }
}
